# -*- coding: utf-8 -*-
import scrapy
import re
from lianjia.items import ZufangItem

class ZufangSpider(scrapy.Spider):
    name = 'zufang'
    allowed_domains = ['bj.lianjia.com']
    start_urls = [
        #'http://bj.lianjia.com/',
        'https://bj.lianjia.com/zufang/anningzhuang1/rt1bd1/pg1/',
    ]
    '''
    'https://bj.lianjia.com/zufang/aolinpikegongyuan11/rt1bd1/',
    'https://bj.lianjia.com/zufang/baishiqiao1/rt1bd1/',
    'https://bj.lianjia.com/zufang/beitaipingzhuang/rt1bd1/',
    'https://bj.lianjia.com/zufang/changpingqita1/rt1bd1/',
    'https://bj.lianjia.com/zufang/changwa/rt1bd1/',
    'https://bj.lianjia.com/zufang/dinghuisi/rt1bd1/',
    'https://bj.lianjia.com/zufang/erlizhuang/rt1bd1/',
    'https://bj.lianjia.com/zufang/gongzhufen/rt1bd1/',
    'https://bj.lianjia.com/zufang/ganjiakou/rt1bd1/',
    'https://bj.lianjia.com/zufang/haidianqita1/rt1bd1/',
    'https://bj.lianjia.com/zufang/haidianbeibuxinqu1/rt1bd1/',
    'https://bj.lianjia.com/zufang/junbo1/rt1bd1/',
    'https://bj.lianjia.com/zufang/liuliqiao1/rt1bd1/',
    'https://bj.lianjia.com/zufang/zhongguancun/rt1bd1/',
    'https://bj.lianjia.com/zufang/zhichunlu/rt1bd1/',
    'https://bj.lianjia.com/zufang/wudaokou/rt1bd1/',
    'https://bj.lianjia.com/zufang/xueyuanlu1/rt1bd1/'
    '''

    def parse(self, response):
	#链家：租房：奥林匹克公园：2居室
	#https://bj.lianjia.com/zufang/aolinpikegongyuan11/l2/
	for sel in response.xpath('//*[@id="house-lst"]/li/div[@class="info-panel"]'):
            item = ZufangItem()
            #小区：
            item['district'] = sel.xpath('div[@class="col-1"]/div[@class="where"]/a/span/text()').extract_first()
            #标题
            item['title'] = sel.xpath('h2/a/text()').extract_first()
            #链接
            item['link'] = sel.xpath('h2/a/@href').extract_first()
            #价格
            item['price'] = sel.xpath('div[@class="col-3"]/div[@class="price"]/span/text()').extract_first()
            #时间单位
            item['time_unit'] = sel.xpath('div[@class="col-3"]/div[@class="price"]/text()').extract_first()
            #面积
            item['area'] = sel.xpath('div[1]/div[1]/span[2]/text()').extract_first()
            #几居室
            item['rooms'] = sel.xpath('div[1]/div[1]/span[1]/span/text()').extract_first()
            #朝向
            #//*[@id="house-lst"]/li[1]/div[2]/div[1]/div[1]/span[3]
            #更新日期
            #//*[@id="house-lst"]/li[2]/div[2]/div[2]/div[2]
            #所在楼层
            #item['floorLoc']
            #总楼层
            #item['floorTotal']
            yield item
        print response.url
        '''
        next_page = response.url
        if next_page is not None:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)
        '''
